package com.css.fxfzxczh.modules.instrumentIntensity.controller;

import com.css.fxfzxczh.base.response.RestResponse;
import com.css.fxfzxczh.modules.instrumentIntensity.entity.InstrumentIntensity;
import com.css.fxfzxczh.modules.instrumentIntensity.service.InstrumentIntensityService;
import com.css.fxfzxczh.modules.instrumentIntensity.service.impl.InstrumentIntensityServiceImpl;
import com.css.fxfzxczh.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author: xzy
 * @CreateTime: 2024/3/22 9:22
 * @Description: 仪器烈度
 */
@RestController
@RequestMapping("/instrumentIntensity")
@Slf4j
public class InstrumentIntensityController {

    @Resource
    InstrumentIntensityService instrumentIntensityService;


    /**
     * 分页
     * @param
     * @return
     */
    @GetMapping("/getPage/{xsemId}")
    public RestResponse getPage(HttpServletRequest request,@PathVariable("xsemId") String xsemId){
        RestResponse restResponse = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=instrumentIntensityService.getPage(curPage,pageSize,xsemId);
            restResponse = RestResponse.succeed(page);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    /**
     * 导入
     * @param file
     * @return
     */
    @PostMapping("importExcel")
    public RestResponse importExcel(@RequestParam("file") MultipartFile file) {
        RestResponse restResponse=null;
        try {
            instrumentIntensityService.importExcel(file);
            restResponse = RestResponse.succeed();
            restResponse.setMessage("导入成功");
        } catch (Exception e) {
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "导入失败";
            }
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @PostMapping("/delete/{ids}")
    public RestResponse delete(@PathVariable("ids") List<String> ids){
        RestResponse restResponse=null;
        try {
            instrumentIntensityService.delete(ids);
            restResponse = RestResponse.succeed();
            restResponse.setMessage("删除成功");
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }


    /**
     * 查询当前地震事件下所有的仪器烈度
     * @param
     * @return
     */
    @GetMapping("/getInstrumentIntensityList")
    public RestResponse<List<InstrumentIntensity>> getInstrumentIntensityList(@RequestParam("xsemId") String xsemId){
        RestResponse restResponse = null;
        try{
            List<InstrumentIntensity> instrumentIntensityList = instrumentIntensityService.getInstrumentIntensityList(xsemId);
            restResponse = RestResponse.succeed(instrumentIntensityList);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }
}
